package com.wangsd.web.coupon.controller;

import com.wangsd.common.basicbean.Result;
import com.wangsd.common.exception.BusinessException;
import com.wangsd.web.MyController;
import com.wangsd.web.coupon.service.BusinessCouponService;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Api(value = "offcialAccount", tags = "微信公众号登录及核销")
@Controller
@RequestMapping("/coupon/offcialAccount")
@Slf4j
public class OffcialAccountController extends MyController {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    BusinessCouponService businessCouponService;
    @Autowired
    RedisTemplate redisTemplate;

    @ApiOperation(value = "进入登录首页", notes = "进入登录首页", httpMethod = "GET")
    @RequestMapping(value = "/loginHtml", method = RequestMethod.GET)
    public Object login(Model model) {
        return "loginHtml";
    }

    @ApiOperation(value = "登录", notes = "登录", httpMethod = "GET")
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public Result login(String username, String password) {
        SysUser sysUser = sysUserService.selectLogin(username, password);
        if (sysUser != null && password.equals(sysUser.getPassword())) {
            String token = UUID.randomUUID().toString();
            redisTemplate.opsForValue().set(token, sysUser, 1, TimeUnit.DAYS);
            return Result.success(token);
        } else {//
            return Result.fail("账号密码错误！");
        }
    }

    @ApiOperation(value = "进入登录首页", notes = "进入登录首页", httpMethod = "GET")
    @RequestMapping(value = "/payHtml", method = RequestMethod.GET)
    public Object payHtml(String token, Model model) {
        model.addAttribute("tokenParam", token);
        return "pay";
    }


    @ApiOperation(value = "核销商家券", notes = "核销商家券", httpMethod = "GET")
    @RequestMapping(value = "/pay", method = RequestMethod.GET)
    @ResponseBody
    public Object pay(String couponCode, String tokenParam) {
        try {
            SysUser user = (SysUser) redisTemplate.opsForValue().get(tokenParam);
            if (user == null) {
                return Result.fail("用户未登录");
            }
//            String code = "9-15-14-20";
            String code = user.getCode();
            businessCouponService.useBusinessCoupon(couponCode, code);
        } catch (BusinessException e) {
            return Result.fail(e.getMsg());
        } catch (IOException e) {
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.success();
    }

}
